// ActionScript file
import Modules.NavigationInfo;
import Modules.PostLogin;

import com.desidea.common.CommonClass;
import com.desidea.common.MenuConstants;
import com.desidea.components.grid.SuperGridEvent;
import com.desidea.components.grid.button.SuperGridButton;
import com.desidea.webservice.PartnerService;
import com.desidea.webservice.ProcessServiceRequestResultEvent;

import flash.events.Event;

import mx.controls.Alert;
import mx.core.Application;
import mx.rpc.events.FaultEvent;

private var _type:String = "";
private var _text:String = "";

private function windowCreated():void{
	this.searchResultGrid.addEventListener(SuperGridEvent.CLICKED, gridButtonColumnClicked);
}

private function gridButtonColumnClicked(event:SuperGridEvent):void{
	handleGridButtonClick(event);
}

private function handleGridButtonClick(event:SuperGridEvent, moduleCode:String = null):void{
	var mode:String =  String(event.data.linkId).toLowerCase();
	var key:String = event.data.data["key"].toString()
	
	
	var menuID:String = (_type == "Accounts")?MenuConstants.MY_ACCOUNT:
							(_type == "Contacts")?MenuConstants.MY_CONTACTS:
								(_type == "Activities")?MenuConstants.MY_ACTIVITIES:
									(_type == "Tasks")?MenuConstants.MY_TASKS:"";
	
	if ( (mode == SuperGridButton.MODE_EDIT || mode == SuperGridButton.MODE_VIEW || mode == SuperGridButton.MODE_VIEWICON) && menuID.length>0){

		var ni:NavigationInfo = new NavigationInfo("",MenuConstants.CMD,
													menuID, key);

		(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(ni);

	}else{
		Alert.show("Invalid Mode or Destination Menu");
	}
}

private function doSearch(event:Event):void{
	
	_type = cmbSearchType.selectedItem.toString();
	_text = txtSearch.text;
	
	var params:String = "OPERATION=SEARCH_BASIC" +
						"&TYPE=" + _type +
						"&TEXT=" + _text +
						"&USERID="+CommonClass.UserID;
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForSearchResults);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
}

private function callForSearchResults(event:ProcessServiceRequestResultEvent):void{
//	BusySpinner.getInstance().stop();
	var response:XML = XML(event.result);
	if(response.RESPONSE.RESULT == "E"){
		Alert.show(response.RESPONSE.MESSAGE);
	}else if(response.RESPONSE.RESULT == 'S'){
		this.searchResultGrid.dataProvider = response;
	}else{
		Alert.show("Unknow Error: Unable to load response");
	}
}
private function partnerServiceFaultHandler(event:FaultEvent):void{
//	BusySpinner.getInstance().stop();
	Alert.show(event.fault.toString());
}
